<template>
	<div>
		<Row>
	        <Col span="24"  style="text-align: center;">
	            <Card style="width:880px;margin: auto;">
	            	<!--加载中样式-->
	            	<Spin size="large" fix v-if="loadingShow"></Spin>
	            	
	                <p slot="title"><slot></slot></p>
	                <Row>
				        <Col span="12" >
				        	<span>选择年级：</span>
				            <Select v-model="grade" style="width:200px">
				                <Option v-for="item in gradelist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>
				        <Col span="12">
				        	<span>输入班号：</span>
							<Input v-model="classes" placeholder="例如：01" style="width:200px">
						        <!--<span slot="prepend">http://</span>-->
						        <span slot="append">班</span>
						    </Input>
				        </Col>
				    </Row>
	                <Row style="margin-top:15px"> 
	                	<!--<Col span="12" >
				        	<span>请选择班主任：</span>
				            <Select v-model="adviser" filterable placeholder="输入快速匹配" style="width:200px">
				                <Option v-for="item in adviserteacherlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>-->
				        
				        <Col span="12">
				        	<span>输入名称：</span>
							<Input v-model="classesname" placeholder="请输入班级名称" style="width:200px">
						        <!--<span slot="prepend">http://</span>-->
						    </Input>
				        </Col>
				    <!--</Row>
				    <Row style="margin-top:15px">-->
				        <Col span="12">
				        	<span style="margin-left: -26px;">年级属性：</span>
				        	<RadioGroup  v-model="branch" type="button" @on-change="changeselect">
				        		<Radio v-for="item in branchlist" :key="item.value" ref="branchselect" :label="item.value">{{item.label}}</Radio>
						       
						    </RadioGroup>
							<!--<Select v-model="branch" style="width:200px">
						        <Option v-for="item in branchlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
						    </Select>-->
				        </Col>
				    </Row>
				    
				    <!--<Row style="margin-top:15px">
				    	<Col span="12" offset="2" style="text-align: left;">
				    		<span>科任老师设置：</span>
				    	</Col>
				    </Row>
				    
				    <Row style="margin-top:15px">
				        <Col span="8" >
				        	<span>语文：</span>
				            <Select v-model="chinese" filterable placeholder="输入快速匹配" style="width:120px">
				                <Option v-for="item in chineseteacherlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>
				        <Col span="8" >
				        	<span>数学：</span>
				            <Select v-model="math" filterable placeholder="输入快速匹配" style="width:120px">
				                <Option v-for="item in mathteacherlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>
				        <Col span="8" >
				        	<span>英语：</span>
				            <Select v-model="english" filterable placeholder="输入快速匹配" style="width:120px">
				                <Option v-for="item in englishteacherlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>
				    </Row>
				    <Row v-if="scienceshow" style="margin-top:15px">
				        <Col span="8" >
				        	<span>物理：</span>
				            <Select v-model="physics" filterable placeholder="输入快速匹配" style="width:120px">
				                <Option v-for="item in physicsteacherlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>
				        <Col span="8" >
				        	<span>化学：</span>
				            <Select v-model="chemistry" filterable placeholder="输入快速匹配" style="width:120px">
				                <Option v-for="item in chemistryteacherlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>
				        <Col span="8" >
				        	<span>生物：</span>
				            <Select v-model="biology" filterable placeholder="输入快速匹配" style="width:120px">
				                <Option v-for="item in biologyteacherlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>
				    </Row>
				    <Row v-if="liberalarts" style="margin-top:15px">
				        <Col span="8" >
				        	<span>历史：</span>
				            <Select v-model="history" filterable placeholder="输入快速匹配" style="width:120px">
				                <Option v-for="item in historyteacherlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>
				        <Col span="8" >
				        	<span>地理：</span>
				            <Select v-model="geography" filterable placeholder="输入快速匹配" style="width:120px">
				                <Option v-for="item in geographyteacherlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>
				        <Col span="8" >
				        	<span>政治：</span>
				            <Select v-model="politics" filterable placeholder="输入快速匹配" style="width:120px">
				                <Option v-for="item in politicsteacherlist" :value="item.value" :key="item.value">{{ item.label }}</Option>
				            </Select>
				        </Col>
				    </Row>-->
				    <Row>
				    	<Col span="24" style="margin-top:15px">
				    		<Button type="ghost" @click="chance" icon="close-round">取消</Button>
				    		<Button type="primary" @click="saveclass" icon="checkmark-round">保存</Button>
				    	</Col>
				    </Row>
				    
	            </Card>
	        </Col>
	    </Row>
	</div>
</template>

<script>
	import api from "@/components/js/Apidomain.js"
	export default{
		props: {
	      	importeditid:"",
	   	},
		data(){
			return{
				loadingShow:false,				//面板菜单获取的加载中显示隐藏
				
				classesname:"",					//班级名称
				adviser:"",						//班主任
				branch:"",						//文理科
				grade:"",						//所属年级
				classes:"",						//班级号    后面1避免关键字
				chinese:"",						//语文老师
				math:"",						//数学老师
				english:"",						//英语老师
				physics:"",						//物理
				chemistry:"",					//化学
				biology:"",						//生物
				history:"",						//历史
				geography:"",					//地理
				politics:"",					//政治
				
				scienceshow:false,					//控制物化生老师的填写的切换显示隐藏（理科）
				liberalarts:false,					//控制史地政老师的填写的切换显示隐藏（文科）
				
				adviserteacherlist: [
//                  {value: 'New York',label: 'New York'},
//                  {value: 'London',label: 'London'},
//                  {value: 'Sydney',label: 'Sydney'},
                ],
                branchlist: [
//					{value: '1',label: '不分科'},
//					{value: '2',label: '理科'},
//                  {value: '3',label: '文科'},   
                ],
                gradelist: [
//					{value: '2',label: '初一年级'},
//                  {value: '3',label: '初二年级'},
//                  {value: '4',label: '初三年级'},
//                  {value: '5',label: '高一年级'},
//                  {value: '6',label: '高二年级'},
//                  {value: '7',label: '高三年级'},  
                ],
                
                chineseteacherlist: [],
                mathteacherlist: [],
                englishteacherlist: [],
                physicsteacherlist: [],
                chemistryteacherlist: [],
                biologyteacherlist: [],
                historyteacherlist: [],
                geographyteacherlist: [],
                politicsteacherlist: [],
			}
		},
		created(){
			this.getselectinfo();
			this.editclass()
//			//console.log(this.importeditid)
		},
		methods:{
			chance:function(){
				this.$emit('chance')
			},
			saveclass:function(){
				this.loadingShow = true;
				//判断importeditid，如果为空，则为新增，如果有值则带值调接口为修改
				if(this.importeditid == ""){
					var classdata = {
						id:"",
						name:this.classesname,					//班级名称
						gradeId:this.grade,						//所属年级
						no:this.classes,						//班级号    
						isBranch:this.branch,						//文理科
						adviserTeacher:this.adviser,						//班主任
						schoolId:"",
						orderNo:"",
						chineseTeacher:this.chinese,						//语文老师
						mathTeacher:this.math,						//数学老师
						englishTeacher:this.english,						//英语老师
						physicsTeacher:this.physics,						//物理
						chemistryTeacher:this.chemistry,					//化学
						biologyTeacher:this.biology,						//生物
						historyTeacher:this.history,						//历史
						geographyTeacher:this.geography,					//地理
						politicsTeacher:this.politics,					//政治
					}
				}else{
					var classdata = {
						id:this.importeditid,
						name:this.classesname,
						gradeId:this.grade,						//所属年级
						no:this.classes,						//班级号    
						isBranch:this.branch,						//文理科
						adviserTeacher:this.adviser,						//班主任
						schoolId:"",
						orderNo:"",
						chineseTeacher:this.chinese,						//语文老师
						mathTeacher:this.math,						//数学老师
						englishTeacher:this.english,						//英语老师
						physicsTeacher:this.physics,						//物理
						chemistryTeacher:this.chemistry,					//化学
						biologyTeacher:this.biology,						//生物
						historyTeacher:this.history,						//历史
						geographyTeacher:this.geography,					//地理
						politicsTeacher:this.politics,					//政治
					}
				}
				
				var self = this;
            	let url = api.apidomain+'/tqes/web/tchClasses/addClasses';
            	this.$http.post(url,classdata).then(function(response) {
					if(response.data.success == true){
						self.loadingShow = false;
						self.$Message.success({
		                    content: '保存成功',
		                    duration: 10,
		                    closable: true
               			})
						//保存成功调用父组件的关闭面板函数
						self.$emit('chance')
		            }else if(response.data.success == false){
		            		self.loadingShow = false;
				            self.$Message.error({
								content: response.data.msg,
								duration: 10,
								closable: true
							});
		            	}
		            	
					})
					.catch(function(error) {
						self.loadingShow = false;
						self.$Message.error({
							content: '系统发生错误：错误码00001',
							duration: 10,
							closable: true
						});
					});
				
			},
			//获取radio里面的值（label），不是value
			changeselect:function(value){ 
				this.$nextTick(function () {
			        let allbranch = this.$refs.branchselect;
					for(let i = 0;i<allbranch.length;i++){
						if(allbranch[i].$el.className == "ivu-radio-wrapper ivu-radio-group-item ivu-radio-wrapper-checked ivu-radio-focus"){
							//replace(/(^\s*)|(\s*$)/g,"") 去掉所有空格
							if(allbranch[i].$el.innerText.replace(/(^\s*)|(\s*$)/g,"") == "理科"){
								this.scienceshow = true;					
								this.liberalarts = false;
								//选择理科后清空文科老师
								this.historyTeacher = "";						//历史
								this.geographyTeacher = "";					//地理
								this.politicsTeacher = "";					//政治
							}else if(allbranch[i].$el.innerText.replace(/(^\s*)|(\s*$)/g,"") == "文科"){
								this.scienceshow = false;					
								this.liberalarts = true;
								//选择文科后清空理科老师
								this.physicsTeacher = "";						//物理
								this.chemistryTeacher = "";					//化学
								this.biologyTeacher = "";						//生物
							}else if(allbranch[i].$el.innerText.replace(/(^\s*)|(\s*$)/g,"") == "不分科"){
								this.scienceshow = true;					
								this.liberalarts = true;
							}
						}
					}
			   })	
			},
			//获取增加班级面板的菜单
			getselectinfo:function(){
				this.loadingShow = true;
				var self = this;
            	let url = api.apidomain+'/tqes/web/tchClasses/getClassesAddSelectParamInfo';
            	this.$http.post(url, {}).then(function(response) {
					if(response.data.success == true){
//						////console.log(response.data.data)

						self.adviserteacherlist = response.data.data.adviserTeacherList;
		                self.branchlist = response.data.data.branchList;
		                self.gradelist = response.data.data.gradeList; 
		                self.chineseteacherlist = response.data.data.chineseTeacherList; 
		                self.mathteacherlist = response.data.data.mathTeacherList;
		                self.englishteacherlist = response.data.data.englishTeacherList;
		                self.physicsteacherlist = response.data.data.physicsTeacherList;
		                self.chemistryteacherlist = response.data.data.chemistryTeacherList;
		                self.biologyteacherlist = response.data.data.biologyTeacherList;
		                self.historyteacherlist = response.data.data.historyTeacherList;
		                self.geographyteacherlist = response.data.data.geographyTeacherList; 
		                self.politicsteacherlist = response.data.data.politicsTeacherList; 

						
	            	}else if(response.data.success == false){
//		            		//console.log(response.data.msg)
	            		self.$Message.error({
							content: response.data.msg,
							duration: 10,
							closable: true
						});	
	            	}
	            	self.loadingShow = false;
				})
				.catch(function(error) {
					self.$Message.error({
							content: '系统发生错误：错误码00002',
							duration: 10,
							closable: true
						});
					self.loadingShow = false;
				});
			},
			editclass:function(){
				//如果等于空那么是新增班级，如果有的话则是编辑，拿id去取年级信息
				
				if(this.importeditid == ""){
					return;
				}else{
					this.loadingShow = true;
					var self = this;
	            	let url = api.apidomain+'/tqes/web/tchClasses/getClassesInfo';
	            	this.$http.post(url, {id:this.importeditid}).then(function(response) {
						if(response.data.success == true){
//							////console.log(response.data.data)
							
							self.adviser = response.data.data.adviserTeacher;						//班主任
							self.branch = response.data.data.isBranch;						//文理科
							self.grade = response.data.data.gradeId;						//所属年级
							self.classes = response.data.data.no;						//班级号    后面1避免关键字
							
							self.chinese = response.data.data.chineseTeacher;						//语文老师
							self.math = response.data.data.mathTeacher;						//数学老师
							self.english = response.data.data.englishTeacher;						//英语老师
							self.physics = response.data.data.physicsTeacher;						//物理
							self.chemistry = response.data.data.chemistryTeacher;					//化学
							self.biology = response.data.data.biologyTeacher;						//生物
							self.history = response.data.data.historyTeacher;						//历史
							self.geography = response.data.data.geographyTeacher;					//地理
							self.politics = response.data.data.politicsTeacher;					//政治
							
							//判断物理和历史的科目是否为空，如果不为空则显示出科目的老师下拉框
							if(self.physics != null && self.history != null){
								self.scienceshow = true;
								self.liberalarts = true;
							}
							if(self.physics != null){
								self.scienceshow = true;
							}else{
								self.scienceshow = false;
							}
							if(self.history != null){
								self.liberalarts = true;
							}else{
								self.liberalarts = false;
							}
							
							self.loadingShow = false;
							
		           }else if(response.data.success == false){
		            		self.$Message.error({
								content: response.data.msg,
								duration: 10,
								closable: true
							});	
				            self.loadingShow = false;
		            	}
		            	
					})
					.catch(function(error) {
						self.$Message.error({
							content: '系统发生错误：错误码00003',
							duration: 10,
							closable: true
						});
						self.loadingShow = false;
					});
				}
			}
		},
		watch:{

			importeditid:function(){
				this.editclass();
			}
		}
	}
</script>

<style>
	.ivu-card-head{
		padding: 10px 16px;
	}
	.ivu-checkbox-group{
		display: inline;
	}
	.ivu-input-group{
		display: inline-table !important;
	}
	input::-ms-input-placeholder{text-align: center;}
	input::-webkit-input-placeholder{text-align: center;}
</style>